package com.helper.dream.a361phonehelper.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.helper.dream.a361phonehelper.R;
import com.helper.dream.a361phonehelper.adapter.ManagerHeaderAdapter;
import com.helper.dream.a361phonehelper.adapter.SuperviseContentAdapter;
import com.helper.dream.a361phonehelper.bean.SuperViseBean;
import com.helper.dream.a361phonehelper.utils.ApiManager;
import com.helper.dream.a361phonehelper.utils.DownString;
import com.helper.dream.a361phonehelper.utils.L;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2016/12/20.
 */

public class GuanliFragment extends Fragment {
    RecyclerView recyclerView, recyclerView_header;
    LinearLayout linearLayout;
    Toolbar toolbar;
    ManagerHeaderAdapter adapter_header;
    SuperviseContentAdapter adapter;
    List<SuperViseBean.FixBean> list;
    List<SuperViseBean.DataBean> list2;
    Handler handler2 = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            adapter_header.notifyDataSetChanged();
            adapter.notifyDataSetChanged();

        }
    };
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        list = new ArrayList<>();
        list2 = new ArrayList<>();
        adapter_header = new ManagerHeaderAdapter(getActivity(), list);

        return inflater.inflate(R.layout.fragment_guanli_layout, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
        loadView();
        downData();
        addHeader();

    }

    private void loadView() {
        adapter = new SuperviseContentAdapter(list2, getActivity());
        LinearLayoutManager l = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(l);
        recyclerView.setAdapter(adapter);
    }

    private void downData() {
        DownString.loadString2(ApiManager.SUPERVISE_URL, new DownString.CallBack2() {
            @Override
            public void success(String msg) {
                Type type = new TypeToken<SuperViseBean>() {
                }.getType();
                Gson gson = new Gson();
                SuperViseBean superviseBean = gson.fromJson(msg, type);
                List<SuperViseBean.FixBean> fix = superviseBean.getFix();
                List<SuperViseBean.DataBean> data = superviseBean.getData();
                list2.clear();
                list.clear();
                list2.addAll(data);
                list.addAll(fix);
                handler2.sendEmptyMessage(123);
                L.d("成功下载完成数据" + list.size() + " size"+ list2.size());
            }

            @Override
            public void fail() {
            }
        });
    }
    private void addHeader() {
        GridLayoutManager grid = new GridLayoutManager(getActivity(), 3, GridLayoutManager.VERTICAL, false);
        recyclerView_header.setLayoutManager(grid);
        recyclerView_header.setAdapter(adapter_header);

    }

    private void init(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        recyclerView_header = (RecyclerView) view.findViewById(R.id.recycler_header);

//        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
//        toolbar.setBackgroundColor(Color.TRANSPARENT);
//        MainActivity activity = (MainActivity) getActivity();
//        ActionBar ab = activity.getSupportActionBar();
//        toolbar.setBackgroundColor(Color.TRANSPARENT);
//        activity.setSupportActionBar(toolbar);
//        toolbar.setNavigationIcon(R.mipmap.user_center_defaut_avatar_01);


    }
}
